//
//  ArrayDataSource.m
//  ifangchou
//
//  Created by mac on 15/11/19.
//  Copyright © 2015年 ifangchou. All rights reserved.
//

#import "ArrayDataSource.h"

@interface ArrayDataSource()

@property (nonatomic,strong) NSString *cellIdentifier;
@property (nonatomic,copy) TableViewCellConfigureBlock configureCellBlock;

@end

@implementation ArrayDataSource

- (id)initWithCellIdentifier:(NSString *)aCellIdentifier andConfigureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
    self = [super init];
    if (self) {
        self.cellIdentifier = aCellIdentifier;
        self.configureCellBlock = [aConfigureCellBlock copy];
    }
    return self;

}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.numberOfSections > 1) {
        NSArray *itemsOfSection = self.items[section];
        return [itemsOfSection count];
    }else{
          return [self.items count] ;
    }
}

static int tag = 10;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    cell.tag = tag;
    tag++;
    
   // NSLog(@"%ld,%ld",indexPath.section,indexPath.row);
    
    if ([[self.items firstObject] isKindOfClass:[NSArray class]])
    {
        NSArray *itemsOfSection = self.items[indexPath.section];
        if (itemsOfSection.count > indexPath.row)
        {
            id item = [itemsOfSection objectAtIndex:indexPath.row];
            self.configureCellBlock(cell,item);
        }
      
    }
    else
    {
        if (self.items.count > indexPath.row)
        {
             id item = [self.items objectAtIndex:indexPath.row];
             self.configureCellBlock(cell,item);
        }
    }
    return cell;
}


@end
